1353B - Two Arrays And Swaps - CodeForces Solution


greedy sortings *800

Please click on ads to support us..

Python Code:

for t in range(int(input())):
    n,k=map(int,input().split())
    l=[int(x) for x in input().split()]
    m=[int(x) for x in input().split()]
    for x in range(k):
        a,b=min(l),max(m)
        if a==b:
            break
        elif a<b:
            i,j=l.index(a),m.index(b)
            l[i],m[j]=m[j],l[i]
    print(sum(l))

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define fofejo ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define ll long long
 
int main()
{
 
    fofejo;
    
    int t;

    cin >> t;

    while(t--) {
        int n,k;
        cin >> n >> k;
        int a[n],b[n];
        
        int a_index,b_index;
        for(int i = 0; i<n; i++) {
            cin >> a[i];
            }
        
        for(int j = 0; j<n; j++) {
            cin >> b[j];
        }

        while(k--){
            int smallest = 31;
            int largest = 0;
            for(int i = 0; i<n; i++) {
                if(a[i]<smallest) {
                    smallest = a[i];
                    a_index = i;
                    
                }
                if(b[i] > largest) {
                    largest = b[i];
                    b_index = i;
                }
            }

            if(smallest < largest) {
                int temp;
                temp = a[a_index];
                a[a_index] = b[b_index];
                b[b_index] = temp;

            }


        }

        int sum=0;

        for(int i=0; i<n; i++) {
            sum += a[i];
        }
        
        cout << sum << "\n";

        



    }

        
    }

    

    


Comments

Submit
0 Comments
More Questions

1635A - Min Or Sum
474A - Keyboard
1343A - Candies
1343C - Alternating Subsequence
1325A - EhAb AnD gCd
746A - Compote
318A - Even Odds
550B - Preparing Olympiad
939B - Hamster Farm
732A - Buy a Shovel
1220C - Substring Game in the Lesson
452A - Eevee
1647B - Madoka and the Elegant Gift
1408A - Circle Coloring
766B - Mahmoud and a Triangle
1618C - Paint the Array
469A - I Wanna Be the Guy
1294A - Collecting Coins
1227A - Math Problem
349A - Cinema Line
47A - Triangular numbers
1516B - AGAGA XOOORRR
1515A - Phoenix and Gold
1515B - Phoenix and Puzzle
155A - I_love_username
49A - Sleuth
1541A - Pretty Permutations
1632C - Strange Test
673A - Bear and Game
276A - Lunch Rush